package com.wctj.api.business.common.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class DefaultMetaObjectHandler implements MetaObjectHandler {
    private static final String CREATED_AT = "createTime";
    private static final String UPDATED_AT = "updateTime";
    private static final String IS_DELETE = "isDeleted";

    @Override
    public void insertFill(MetaObject metaObject) {
        Object createdAt = getFieldValByName(CREATED_AT, metaObject);
        Object updatedAt = getFieldValByName(UPDATED_AT, metaObject);
        Object isDelete = getFieldValByName(IS_DELETE, metaObject);
        if (createdAt == null) {
            setFieldValByName(CREATED_AT, LocalDateTime.now(), metaObject);
        }
        if (updatedAt == null) {
            setFieldValByName(UPDATED_AT, LocalDateTime.now(), metaObject);
        }
        if (isDelete == null) {
            setFieldValByName(IS_DELETE, 0, metaObject);
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Object createdAt = getFieldValByName(UPDATED_AT, metaObject);
        if (createdAt == null) {
            setFieldValByName(UPDATED_AT, LocalDateTime.now(), metaObject);
        }
    }
}